package com.ruoyi.work.service.impl;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.ICommonEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.ruoyi.work.domain.*;
import com.ruoyi.work.mapper.*;
import com.ruoyi.work.service.IWorkPaperService;
import com.ruoyi.work.service.PriceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;



/**
 * 稿件管理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-12-26
 */
@Service
public class WorkPaperServiceImpl implements IWorkPaperService {
    @Autowired
    private WorkPaperMapper workPaperMapper;
    @Autowired
    private WorkCustomerMapper workCustomerMapper;
    @Autowired
    private WorkMediaAppMapper workMediaAppMapper;
    @Autowired
    private WorkMediaNetMapper workMediaNetMapper;
    @Autowired
    private WorkMediaWxMapper workMediaWxMapper;
    @Autowired
    private WorkMediaWbMapper workMediaWbMapper;
    @Autowired
    private WorkMediaFlowMapper workMediaFlowMapper;
    @Autowired
    private WorkMediaPlaneMapper workMediaPlaneMapper;
    @Autowired
    private SysDictDataMapper sysDictDataMapper;


    /**
     * 查询稿件管理
     *
     * @param paperId 稿件管理主键
     * @return 稿件管理
     */
    @Override
    public WorkPaper selectWorkPaperByPaperId(Long paperId) {
        return workPaperMapper.selectWorkPaperByPaperId(paperId);
    }

    /**
     * 查询稿件管理列表
     *
     * @param workPaper 稿件管理
     * @return 稿件管理
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<WorkPaper> selectWorkPaperList(WorkPaper workPaper) {
        return workPaperMapper.selectWorkPaperList(workPaper);
    }
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<WorkPaper> selectWorkPaperGroupList(WorkPaper workPaper) {
        return workPaperMapper.selectWorkPaperGroupList(workPaper);
    }
    @Override
    public List<WorkPaper> selectWorkPaperLists(WorkPaper workPaper) {
        return workPaperMapper.selectWorkPaperList(workPaper);
    }
    @Override
    public List<WorkPaper> selectWorkPaperGroupLists(WorkPaper workPaper) {
        return workPaperMapper.selectWorkPaperGroupList(workPaper);
    }
    /**
     * 批量查询稿件管理
     *
     * @param paperIds 需要查询的数据主键集合
     * @return 结果
     */
    @Override
    public List<WorkPaper> selectWorkPaperByPaperIds(String[] paperIds) {

        return workPaperMapper.selectWorkPaperByPaperIds(paperIds);
    }
    @Override
    public List<WorkPaper> selectWorkPaperByBatchId(Long batchId){

        return workPaperMapper.selectWorkPaperByBatchId(batchId);
}
    /**
     * 新增稿件管理
     *
     * @param workPaper 稿件管理
     * @return 结果
     */
    @Override
    public int insertWorkPaper(WorkPaper workPaper) {
        workPaper.setCreateTime(DateUtils.getNowDate());
        if(workPaper.getCustomerId()!=null && workPaper.getCustomerId()>0) {
            WorkCustomer customer = workCustomerMapper.selectWorkCustomerByCustomerId(workPaper.getCustomerId());
            workPaper.setCustomerName(customer.getCustomerName());
            workPaper.setCompanyName(customer.getCompanyName());
        }

        //TODO优化枚举策略

        if (workPaper.getPaperType().equals("1")) {
            WorkMediaNet workMediaNet = workMediaNetMapper.selectWorkMediaNetByMediaId(workPaper.getMediaId());
            workPaper.setMediaName(workMediaNet.getMediaName());
            workPaper.setMediaId(workMediaNet.getMediaId());
            workPaper.setContactId(workMediaNet.getContactId());
            workPaper.setContacts(workMediaNet.getContacts());
            workPaper.setPaperPrice(workMediaNet.getPrice());
            workPaper.setCost(workMediaNet.getPrice());
            workPaper.setActualCost(workMediaNet.getPrice());

        } else if (workPaper.getPaperType().equals("2")) {
            WorkMediaWb workMediaWb = workMediaWbMapper.selectWorkMediaWbByMediaId(workPaper.getMediaId());
            workPaper.setMediaName(workMediaWb.getMediaName());
            workPaper.setMediaId(workMediaWb.getMediaId());
            workPaper.setContactId(workMediaWb.getContactId());
            workPaper.setContacts(workMediaWb.getContacts());

/*            if(workPaper.getPriceType().equals(PriceType.WDIRECT.getCode())){
                workPaper.setPaperPrice(workMediaWb.getwDirectPrice());
                workPaper.setCost(workMediaWb.getwDirectPrice());
                workPaper.setActualCost(workMediaWb.getwDirectPrice());
            }else if(workPaper.getPriceType().equals(PriceType.WFORWARD.getCode())){
                workPaper.setPaperPrice(workMediaWb.getwForwardPrice());
                workPaper.setCost(workMediaWb.getwForwardPrice());
                workPaper.setActualCost(workMediaWb.getwForwardPrice());
            }else if(workPaper.getPriceType().equals(PriceType.DIRECT.getCode())){
                workPaper.setPaperPrice(workMediaWb.getDirectPrice());
                workPaper.setCost(workMediaWb.getDirectPrice());
                workPaper.setActualCost(workMediaWb.getDirectPrice());
            }else if(workPaper.getPriceType().equals(PriceType.FORWARD.getCode())){
                workPaper.setPaperPrice(workMediaWb.getForwardPrice());
                workPaper.setCost(workMediaWb.getForwardPrice());
                workPaper.setActualCost(workMediaWb.getForwardPrice());
            }*/
            BigDecimal price = ICommonEnum.getEnmuByCode(workPaper.getPriceType(),PriceType.class).getFunction().apply(workMediaWb);
            workPaper.setPaperPrice(price );
            workPaper.setCost(price);
            workPaper.setActualCost(price);

        } else if (workPaper.getPaperType().equals("3")) {
            WorkMediaWx workMediaWx = workMediaWxMapper.selectWorkMediaWxByMediaId(workPaper.getMediaId());
            workPaper.setMediaName(workMediaWx.getMediaName());
            workPaper.setMediaId(workMediaWx.getMediaId());
            workPaper.setContactId(workMediaWx.getContactId());
            workPaper.setContacts(workMediaWx.getContacts());

           /* if(workPaper.getPriceType().equals(PriceType.LEAD)){
                workPaper.setPaperPrice(workMediaWx.getLeadPrice());
                workPaper.setCost(workMediaWx.getLeadPrice());
                workPaper.setActualCost(workMediaWx.getLeadPrice());
            }else if(workPaper.getPriceType().equals(PriceType.SECOND)){
                workPaper.setPaperPrice(workMediaWx.getSecondPrice());
                workPaper.setCost(workMediaWx.getSecondPrice());
                workPaper.setActualCost(workMediaWx.getSecondPrice());
            }*/
            BigDecimal price = ICommonEnum.getEnmuByCode(workPaper.getPriceType(),PriceType.class).getFunction().apply(workMediaWx);
            workPaper.setPaperPrice(price );
            workPaper.setCost(price);
            workPaper.setActualCost(price);
        } else if (workPaper.getPaperType().equals("4")) {
            WorkMediaPlane workMediaPlane = workMediaPlaneMapper.selectWorkMediaPlaneByMediaId(workPaper.getMediaId());
            workPaper.setMediaName(workMediaPlane.getMediaName());
            workPaper.setMediaId(workMediaPlane.getMediaId());
            workPaper.setContactId(workMediaPlane.getContactId());
            workPaper.setContacts(workMediaPlane.getContacts());

        } else if (workPaper.getPaperType().equals("5")) {
            WorkMediaApp workMediaApp = workMediaAppMapper.selectWorkMediaAppByMediaId(workPaper.getMediaId());
            workPaper.setMediaName(workMediaApp.getMediaName());
            workPaper.setPlatform(workMediaApp.getPlatform());
            workPaper.setMediaId(workMediaApp.getMediaId());
            workPaper.setContactId(workMediaApp.getContactId());
            workPaper.setContacts(workMediaApp.getContacts());

            workPaper.setPaperPrice(workMediaApp.getPrice());
            workPaper.setCost(workMediaApp.getPrice());
            workPaper.setActualCost(workMediaApp.getPrice());

        } else if (workPaper.getPaperType().equals("6")) {
            WorkMediaFlow workMediaFlow = workMediaFlowMapper.selectWorkMediaFlowByMediaId(workPaper.getMediaId());
            workPaper.setMediaName(workMediaFlow.getMediaName());
            workPaper.setPlatform(workMediaFlow.getPlatform());
            workPaper.setMediaId(workMediaFlow.getMediaId());
            workPaper.setContactId(workMediaFlow.getContactId());
            workPaper.setContacts(workMediaFlow.getContacts());

        }

        return workPaperMapper.insertWorkPaper(workPaper);
    }

    /**
     * 修改稿件管理
     *
     * @param workPaper 稿件管理
     * @return 结果
     */
    @Override
    public int updateWorkPaper(WorkPaper workPaper) {
        workPaper.setUpdateTime(DateUtils.getNowDate());
        return workPaperMapper.updateWorkPaper(workPaper);
    }

    /**
     * 删除稿件发票号
     *
     * @param invoiceId 稿件管发票号
     * @return 结果
     */
    public int updateWorkPaperInvoice(Long invoiceId) {
        return workPaperMapper.updateWorkPaperInvoice(invoiceId);
    }

    /**
     * 批量删除稿件管理
     *
     * @param paperIds 需要删除的稿件管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkPaperByPaperIds(String paperIds) {
        return workPaperMapper.deleteWorkPaperByPaperIds(Convert.toStrArray(paperIds));
    }

    /**
     * 删除稿件管理信息
     *
     * @param paperId 稿件管理主键
     * @return 结果
     */
    @Override
    public int deleteWorkPaperByPaperId(Long paperId) {
        return workPaperMapper.deleteWorkPaperByPaperId(paperId);
    }
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<WorkPaper>  selectPaperNoCancel(Map<String,Object> map){
        return workPaperMapper.selectPaperNoCancel(map);
    }
    /**
     * 销账查询销账部分稿件
     *
     * @param incomingId 来款主键
     * @return 结果
     */
    @Override
    @DataScope(deptAlias = "d", userAlias = "u")
    public List<WorkPaper> selectPaperByCancel(Map<String,Object> map) {
        return workPaperMapper.selectPaperByCancel(map);
    }

    @Override
    public int updateWorkPaperCancelTime(String[] invoiceIds) {
        return workPaperMapper.updateWorkPaperCancelTime(invoiceIds);
    }
    @Override
    public int updateWorkPaperBackReq(String[] requestIds) {
        return workPaperMapper.updateWorkPaperBackReq(requestIds);
    }
}
